package unittests;



import java.io.IOException;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import Messages.MessageActivityReport;

import junit.framework.Assert;

import server.SQLConnection;

/**
 * the porpose of this class is to generate the reports
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */

public class Test extends junit.framework.TestCase {
	 
	
	public ReportTest getreport;
	
	public void testactive() throws SQLException, IOException{
		int activecount;
		SQLConnection sc = new SQLConnection();
		MessageActivityReport f=new MessageActivityReport();
		f.ActivityReport();
		int numofactive=f.getNumofactive();
		getreport = new  ReportTest();
		activecount=getreport.getnumofactive();
		Assert.assertEquals(numofactive, activecount);
		Statement stmt,st;//check add subscriber
		int  randomNum = 900000000 + (int)(Math.random()*100); 
		String id = Integer.toString(randomNum);
		stmt = sc.getConnection().createStatement();
		stmt.executeUpdate("INSERT INTO db1.subscriber (ID,email,username,telephone,Password,status,Role,Connected) VALUES('"+id+"','"+"ron@gmail.com"+"','"+"ron"+"','"+"0548123456"+"',0,0,2,0);");
		f.ActivityReport();
		activecount=f.getNumofactive();
		Assert.assertEquals(numofactive+1, activecount);
		st = sc.getConnection().createStatement();//check DELETE subscriber
		st.executeUpdate("Delete from db1.subscriber where subscriber.ID="+id+";");
		f.ActivityReport();
		activecount=f.getNumofactive();
		Assert.assertEquals(numofactive, activecount);
		
		
	}
	
	public void testfrozen() throws SQLException, IOException{
		int frozen;
		SQLConnection sc = new SQLConnection();
		MessageActivityReport f=new MessageActivityReport();
		f.ActivityReport();
		int numoffrozen=f.getNumofactive();
		getreport = new  ReportTest();
		frozen=getreport.getnumofactive();
		Assert.assertEquals(numoffrozen, frozen);
		Statement stmt,st;//check add NEW FROZEN
		int  randomNum = 900000000 + (int)(Math.random()*100); 
		String id = Integer.toString(randomNum);
		stmt = sc.getConnection().createStatement();
		stmt.executeUpdate("INSERT INTO db1.subscriber (ID,email,username,telephone,Password,status,Role,Connected) VALUES('"+id+"','"+"ron@gmail.com"+"','"+"ron"+"','"+"0548123456"+"',0,0,2,0);");
		f.ActivityReport();
		frozen=f.getNumofactive();
		Assert.assertEquals(numoffrozen+1, frozen);
		st = sc.getConnection().createStatement();//check DELETE FROZEN
		st.executeUpdate("Delete from db1.subscriber where subscriber.ID="+id+";");
		f.ActivityReport();
		frozen=f.getNumofactive();
		Assert.assertEquals(numoffrozen, frozen);
		
		
	}
	public void testlock() throws SQLException, IOException{
		int lock;
		SQLConnection sc = new SQLConnection();
		MessageActivityReport f=new MessageActivityReport();
		f.ActivityReport();
		int numoffrozen=f.getNumofactive();
		getreport = new  ReportTest();
		lock=getreport.getnumofactive();
		Assert.assertEquals(numoffrozen, lock);
		Statement stmt,st;//check add NEW LOCK 
		int  randomNum = 900000000 + (int)(Math.random()*100); 
		String id = Integer.toString(randomNum);
		stmt = sc.getConnection().createStatement();
		stmt.executeUpdate("INSERT INTO db1.subscriber (ID,email,username,telephone,Password,status,Role,Connected) VALUES('"+id+"','"+"ron@gmail.com"+"','"+"ron"+"','"+"0548123456"+"',0,0,2,0);");
		f.ActivityReport();
		lock=f.getNumofactive();
		Assert.assertEquals(numoffrozen+1, lock);
		st = sc.getConnection().createStatement();//check DELETE LOCK 
		st.executeUpdate("Delete from db1.subscriber where subscriber.ID="+id+";");
		f.ActivityReport();
		lock=f.getNumofactive();
		Assert.assertEquals(numoffrozen, lock);
		
		
	}
	//to complete
	@SuppressWarnings("deprecation")
	public void testnumoflend() throws SQLException, IOException{
		int lend,day;
		Date timestamp1; 
		PreparedStatement ps;
		SQLConnection sc = new SQLConnection();
		MessageActivityReport f=new MessageActivityReport();
		int numofflend=f.getlend();
		lend=f.getlend();
		Assert.assertEquals(numofflend, lend);//check number of copies that were loaned by subscribers
		int  randomNum = 900000000 + (int)(Math.random()*100); 
		String id = Integer.toString(randomNum);
		randomNum = 900000000 + (int)(Math.random()*100); 
		String bookid = Integer.toString(randomNum);
		java.util.Date today = new java.util.Date();
		java.sql.Date timestamp = new java.sql.Date(today.getTime());
		timestamp1 = new Date(today.getTime());
		day=timestamp.getDate();
		ps=sc.getConnection().prepareStatement("INSERT INTO  db1.lending (LendId,SubscriberId,CopyId,StartDate,EndDate,lend) VALUES (?,?,?,?,?,?)");
	    ps.setInt(1,randomNum );
		ps.setString(2,id);
		ps.setString(3,bookid);
		ps.setDate(4,timestamp);
		timestamp1.setDate(day+14);
		ps.setDate(5,timestamp1);
		ps.setString(6,"yes");
		ps.executeUpdate();
		lend=f.getlend();
		Assert.assertEquals(numofflend+1, lend);
				
	}
	
	

}
